package gui;

import ge.escola.Disciplina;
import ge.escola.Serie;
import ge.escola.Turma;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;

import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class SelecionaTurma extends javax.swing.JPanel {
	private JLabel jLabel1;
	private JTextArea textArea;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JTextField caixaTurma;
	private JButton buttonExcluir;
	private JButton buttonVoltar;
	private JButton buttonAcessar;
	private JTextField caixaSerie;

	public static Turma turma;
	private String nomeTurma,nomeSerie;
	
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	
	public SelecionaTurma() {
		super();
		initGUI();
		
		atualizaTurma();
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			this.setSize(800, 600);
			this.setLayout(null);
			this.setBackground(new java.awt.Color(204,204,255));
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Seleciona turma");
				jLabel1.setBounds(217, 20, 353, 71);
				jLabel1.setFont(new java.awt.Font("Arial",0,48));
				jLabel1.setForeground(new java.awt.Color(0,102,102));
			}
			{
				textArea = new JTextArea();
				this.add(textArea);
				textArea.setBounds(48, 125, 343, 422);
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("Nome da turma");
				jLabel2.setBounds(509, 148, 127, 22);
				jLabel2.setFont(new java.awt.Font("Arial",0,18));
			}
			{
				jLabel3 = new JLabel();
				this.add(jLabel3);
				jLabel3.setText("Nome da serie da turma");
				jLabel3.setFont(new java.awt.Font("Arial",0,18));
				jLabel3.setBounds(440, 202, 196, 22);
			}
			{
				caixaTurma = new JTextField();
				this.add(caixaTurma);
				caixaTurma.setBounds(654, 146, 77, 28);
				caixaTurma.setFont(new java.awt.Font("Arial",0,16));
			}
			{
				caixaSerie = new JTextField();
				this.add(caixaSerie);
				caixaSerie.setFont(new java.awt.Font("Arial",0,16));
				caixaSerie.setBounds(654, 196, 77, 28);
			}
			{
				buttonAcessar = new JButton();
				this.add(buttonAcessar);
				buttonAcessar.setText("ACESSAR");
				buttonAcessar.setBounds(468, 287, 100, 21);
				buttonAcessar.setFont(new java.awt.Font("Arial",0,12));
				buttonAcessar.setBackground(new java.awt.Color(255,255,255));
				buttonAcessar.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				buttonAcessar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonAcessarMouseClicked(evt);
					}
				});
			}
			{
				buttonExcluir = new JButton();
				this.add(buttonExcluir);
				buttonExcluir.setText("EXCLUIR");
				buttonExcluir.setFont(new java.awt.Font("Arial",0,12));
				buttonExcluir.setBounds(619, 287, 100, 21);
				buttonExcluir.setBackground(new java.awt.Color(255,255,255));
				buttonExcluir.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				buttonExcluir.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonExcluirMouseClicked(evt);
					}
				});
			}
			{
				buttonVoltar = new JButton();
				this.add(buttonVoltar);
				buttonVoltar.setText("VOLTAR");
				buttonVoltar.setFont(new java.awt.Font("Arial",0,12));
				buttonVoltar.setBounds(643, 507, 100, 21);
				buttonVoltar.setBackground(new java.awt.Color(255,255,255));
				buttonVoltar.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				buttonVoltar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonVoltarMouseClicked(evt);
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void buttonAcessarMouseClicked(MouseEvent evt) {
		nomeSerie = caixaSerie.getText();
		nomeTurma = caixaTurma.getText();
		if(checaTurma()){
			GerenciadorDeJanelas.alteraTurma = new AlteraTurma();
			GerenciadorDeJanelas.window.setVisible(false);
	        GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.alteraTurma);
	        GerenciadorDeJanelas.window.setVisible(true);
		}else{
			JOptionPane.showMessageDialog(null, "Turma ou serie nao cadastrada.");
		}
	}
	
	private void buttonExcluirMouseClicked(MouseEvent evt) {
		nomeSerie = caixaSerie.getText();
		nomeTurma = caixaTurma.getText();
		boolean flag = checaTurma();
		if(flag){
			GerenciadorDeJanelas.todasTurmas.remove(turma);
			String xml = GerenciadorDeJanelas.ARQUIVADOR.toXML(GerenciadorDeJanelas.todasTurmas);
			GerenciadorDeJanelas.gravar(GerenciadorDeJanelas.arquivoDeTurmas,xml);
			JOptionPane.showMessageDialog(null, "Turma removida com sucesso!");
		}else{
			JOptionPane.showMessageDialog(null, "Turma nao cadastrada.");
		}
	}
	
	private void buttonVoltarMouseClicked(MouseEvent evt) {
		GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.menuAltera);
        GerenciadorDeJanelas.window.setVisible(true);
	}
	
	private boolean checaTurma(){
		for(Turma turma2:GerenciadorDeJanelas.todasTurmas){
			if(nomeTurma.equals(turma2.getNome()) || turma2.getSerie().getNome().equals(nomeSerie)){
				turma = turma2;
				return true;
			}
		}
		return false;
	}
	
	private void atualizaTurma(){
		String saida = "";
		if(GerenciadorDeJanelas.todasTurmas.size()<=0){
			saida = "Nenhuma turma cadastrada.";
		}else{
			for(Turma turma2:GerenciadorDeJanelas.todasTurmas){
				saida+=turma.toString() + "\n";
			}
		}
		textArea.setEditable(true);
		textArea.setText(saida);
		textArea.setEditable(false);
		
	}

}
